import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import { ElLoading, ElMessage } from 'element-plus'
let loading: any;
const startLoading = () => {//开始loading
    interface Options {
        Lock: boolean;
        text: string;
        background: string;
    }
    const options: Options = {//loading配置项
        Lock: true,
        text: '加载中...',
        background: 'rgba(0,0,0,0.7)'
    };
    loading = ElLoading.service(options);
}
const endLoading = () => {//结束loading
    loading.close();
}
//请求拦截
axios.interceptors.request.use((config: AxiosRequestConfig) => {
    startLoading()
    return config;
})

//响应拦截
axios.interceptors.response.use((response: AxiosResponse<any>) => {
    endLoading()
    return response.data
}, error => {
    endLoading()
    ElMessage.warning(error.response.data.error);
    return Promise.reject(error);
})

export default axios;